/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package triploa;
import de.enough.polish.util.TextUtil;
import org.kxml2.io.*;
import org.kxml2.kdom.*;
import org.xmlpull.v1.*;
import java.io.*;
import java.util.Vector;

/**
 *
 * @author User
 */
public class Travel {
    private int id;
    private String from;
    private String to;
    private String date;
    private String time;
    private boolean acceptSmokers=false;
    private int freeSeat=1;
    private float cost=-1;
    private int userId;
    private String owner;
    private float feedbacks;
    
    private boolean accomplished;
        private float deviationRadius;
        private boolean draft;
        private boolean requireLicensed;
        private float realBudget;
        private int realTime;
        private int estimatedTime;
        private String reservationExpirationDate;

        //Note per il momento resta una stringa
        
        private String notes;
        private Vector reservatedUser;
        private boolean isBooked;
        private boolean isInFavorites ;
        private boolean isOffered ;
    
    Vector stages=null;
    // rest travel tag
    private static final String TRAVEL_TAG="a:TripInfo";
    private static final String TRAVEL_DETAIL_TAG="a:FullTripInfo";
    private static final String FROM_TAG="a:Departure";
    private static final String TO_TAG="a:Arrival";
    private static final String ID_TAG="a:ID";
    private static final String DATE_TAG="a:DepartureDate";
    private static final String SMOKERS_TAG="a:AcceptSmokers";
    private static final String FREE_SEAT_TAG="a:FreeSeats";
    private static final String COST_TAG="a:EstimatedBudget";
  
    private static final String OWNER_TAG="a:Owner";
    private static final String USER_ID_TAG="a:UserID";
    private static final String USER_NICK_TAG="a:Username";
    private static final String USER_FEED_TAG="a:NumericFeedbacks";
    
    private static final String ACCOMPLISHED_TAG="a:Accomplished";
    private static final String DEVIATION_TAG="a:DeviationRadius";
    private static final String DRAFT_TAG="a:Draft";
    private static final String BOOKED_TAG="a:IsBooked";
    private static final String OFFERED_TAG="a:IsOffered";
    private static final String FAVORITE_TAG="a:IsInFavorite";
    private static final String EXPIRING_TAG="a:ReservationExpirationDate";
    private static final String REAL_TIME_TAG="a:RealTime";
    private static final String REAL_BUGDET_TAG="a:RealBudget";
    private static final String NOTE_TAG="a:Notes";
    private static final String LICENSE_TAG="a:RequireLicensed";
    private static final String STAGES_TAG="a:Stages";
    
    private static final String REPETITION_TAG="a:IsFrequent";
    private static final String PASSENGERS_TAG="a:ReservatedUsers";
    private static final String ESTIMATED_TIME_TAG="a:EstimatedTime";
    private boolean repetition;
      
    
    public Travel(int id,String from,String to,String date,boolean smokers,int seat)
    {
        this.id=id;
        this.from=from;
        this.to=to;
        this.date=date;
        acceptSmokers=smokers;
        freeSeat=seat;
    }
    private Travel()
    {
        stages=new Vector();
        id=-1;
    }
    /**
 * call for getting a new instance of this class from xml
 * @param p Parser
 * @return a new instance of FeedBack
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
    public static Travel create(KXmlParser p) throws XmlPullParserException, IOException {
        Travel up = new Travel();
        up.update(p);
        if(up.from==null&&up.to==null&&up.date==null&&up.id==-1)
        {
            return null;
        }
        return up;
    }
    public String getFrom()
    {
        return from;
    }
    public String getTo()
    {
        return to;
    }
    public String getDate()
    {
        return date;
    }
    public int getID()
    {
        return id;
    }
    
    /**
 * Update this object class from xml parser
 * @param p Parser
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
    public void update(KXmlParser p) throws XmlPullParserException, IOException {
        boolean exit = false, travel = false;
        int token = -1;
        String n="",lasttag=n;
        while (!exit) {
            token = p.next();
            switch (token) {
                case KXmlParser.START_DOCUMENT:
                    break;
                case KXmlParser.END_DOCUMENT:
                    exit = true;  //exit form while
                    break;
                case KXmlParser.START_TAG:
                    System.out.println(p.getName());
                    if (p.getName().equals(TRAVEL_TAG)||p.getName().equals(TRAVEL_DETAIL_TAG)) {
                        travel = true;
                    }
                    if (travel) {
                        lasttag=p.getName();
                        if(lasttag.equals(STAGES_TAG)) //lettura degli stage
                        {
                            Stage app=Stage.create(p);;
                            while(app!=null){ //parse stages
                                stages.addElement(app);
                                app=Stage.create(p);
                            }
                        }
                        if(lasttag.equals(PASSENGERS_TAG)) //lettura dei passeggeri
                        {
                            this.reservatedUser=UserList.create(p).results;
                        }
                    }
                    break;
                case KXmlParser.END_TAG:
                    if (travel) {
                        if (p.getName().equals(TRAVEL_TAG)||p.getName().equals(TRAVEL_DETAIL_TAG)) {
                            travel = false;
                            exit = true;
                            break;
                        }
                        
                    }
                    lasttag=n;
                    break;
                case KXmlParser.TEXT:
                    if (lasttag.equals(ID_TAG)) {
                        id = Integer.parseInt(p.getText());
                    }
                    if (lasttag.equals(this.ESTIMATED_TIME_TAG)) {
                        setEstimatedTime(Integer.parseInt(p.getText()));
                    }
                    if (lasttag.equals(FROM_TAG)) {
                        from = p.getText();
                    }
                    if (lasttag.equals(TO_TAG)) {
                        to = p.getText();
                    }
                    if (lasttag.equals(NOTE_TAG)) {
                        to = p.getText();
                    }
                    if (lasttag.equals(DATE_TAG)) {
                        String dt= p.getText();
                        int t=dt.indexOf("T");
                        date = dt.substring(0, t);
                        setTime(dt.substring(t + 1));
                    }
                    if (lasttag.equals(FREE_SEAT_TAG)) {
                        freeSeat = Integer.parseInt(p.getText());
                    }
                    if (lasttag.equals(SMOKERS_TAG)) {
                        if(p.getText().equals("true"))
                            acceptSmokers = true;
                    }
                    if (lasttag.equals(COST_TAG)) {
                        cost = Float.parseFloat(p.getText());
                    }
                    if (lasttag.equals(USER_ID_TAG)) {
                        setUserId(Integer.parseInt(p.getText()));
                    }
                    if (lasttag.equals(USER_NICK_TAG)) {
                        setOwner(p.getText());
                    }
                    if (lasttag.equals(USER_FEED_TAG)) {
                        setFeedbacks(Float.parseFloat(p.getText()));
                    }
                    if (lasttag.equals(this.DEVIATION_TAG)) {
                        this.deviationRadius=Float.parseFloat(p.getText());
                    }
                    if (lasttag.equals(this.REAL_BUGDET_TAG)) {
                        this.realBudget=Float.parseFloat(p.getText());
                    }
                    if (lasttag.equals(this.REAL_TIME_TAG)) {
                        this.realTime=Integer.parseInt(p.getText());
                    }
                    if (lasttag.equals(this.REPETITION_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            this.setRepetition(true);
                        }
                    }
                    if (lasttag.equals(OFFERED_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            isOffered=true;
                        }
                    }
                    if (lasttag.equals(BOOKED_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            isBooked=true;
                        }
                    }
                    if (lasttag.equals(FAVORITE_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            this.isInFavorites=true;
                        }
                    }
                    if (lasttag.equals(DRAFT_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            this.draft=true;
                        }
                    }
                    if (lasttag.equals(LICENSE_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            this.requireLicensed=true;
                        }
                    }
                    if (lasttag.equals(this.ACCOMPLISHED_TAG)) {
                        if(p.getText().equals("true"))
                        {
                            this.accomplished=true;
                            
                        }
                    }
                    if (lasttag.equals(EXPIRING_TAG)) {
                        reservationExpirationDate= p.getText().replace('T', ' ');
                    }
                    break;
            }
        }
    }

    public boolean isAcceptSmokers() {
        return acceptSmokers;
    }

    public int getFreeSeat() {
        return freeSeat;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public float getCost() {
        return cost;
    }

    public int getUserId() {
        return userId;
    }

    public String getOwner() {
        return owner;
    }

    public float getFeedbacks() {
        return feedbacks;
    }

    public void setAccomplished(boolean accomplished) {
        this.accomplished = accomplished;
    }

    public void setDeviationRadius(float deviationRadius) {
        this.deviationRadius = deviationRadius;
    }

    public void setDraft(boolean draft) {
        this.draft = draft;
    }

    public void setRequireLicensed(boolean requireLicensed) {
        this.requireLicensed = requireLicensed;
    }

    public void setRealBudget(float realBudget) {
        this.realBudget = realBudget;
    }

    public void setRealTime(int realTime) {
        this.realTime = realTime;
    }

    public void setReservationExpirationDate(String ReservationExpirationDate) {
        this.reservationExpirationDate = ReservationExpirationDate;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public void setReservatedUser(Vector reservatedUser) {
        this.reservatedUser = reservatedUser;
    }

    public void setIsBooked(boolean isBooked) {
        this.isBooked = isBooked;
    }

    public void setIsInFavorites(boolean IsInFavorites) {
        this.isInFavorites = IsInFavorites;
    }

    public void setIsOffered(boolean isOffered) {
        this.isOffered = isOffered;
    }

    public void setCost(float cost) {
        this.cost = cost;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public void setFeedbacks(float feedbacks) {
        this.feedbacks = feedbacks;
    }

    public boolean isAccomplished() {
        return accomplished;
    }

    public float getDeviationRadius() {
        return deviationRadius;
    }

    public boolean isDraft() {
        return draft;
    }

    public boolean isRequireLicensed() {
        return requireLicensed;
    }

    public float getRealBudget() {
        return realBudget;
    }

    public int getRealTime() {
        return realTime;
    }

    public String getReservationExpirationDate() {
        return reservationExpirationDate;
    }

    public String getNotes() {
        return notes;
    }

    public Vector getReservatedUser() {
        return reservatedUser;
    }

    public boolean isIsBooked() {
        return isBooked;
    }

    public boolean isIsInFavorites() {
        return isInFavorites;
    }

    public boolean isIsOffered() {
        return isOffered;
    }
    public String getCityFrom()
    {
        if(from==null) return "Errore";
        String app[]=TextUtil.split(from,',');
        if(app.length>=3&&app[app.length-3]!=null)
        {
            return app[app.length-3]; 
        }
        else return from;
        //return TextUtil.split(from,',')[1];
        /*String citta="";
        if(from!=null)
        {
            
            int finevia=from.indexOf(',');
            int finecitta=from.indexOf(',', finevia+1);
            citta=from.substring(finevia+1, finecitta-1);
            System.out.println("Get city departure "+from+" Result "+citta);
        }
        return  citta;*/
    }
    public String getCityTo()
    {
        //return to;
        if(to==null) return "Errore";
        String app[]=TextUtil.split(to,',');
        if(app.length>=3&&app[app.length-3]!=null)
        {
            return app[app.length-3]; 
        }
        else return to;
        /*String citta="";
        if(to!=null)
        {
            
            int finevia=to.indexOf(',');
            int finecitta=to.indexOf(',', finevia+1);
            citta=from.substring(finevia+1, finecitta-1);
            System.out.println("Get city destination "+to+" Result "+citta);
        }
        return  citta;*/
    }
    public String toXml()
    {
        Document doc=new Document();
        Element trip=new Element();
        trip.setName(this.TRAVEL_DETAIL_TAG);
        Element app=new Element();
        app.setName(this.TO_TAG);
        app.addChild(app.TEXT, to);
        trip.addChild(trip.ELEMENT, app);
        app=new Element();
        app.setName(this.FROM_TAG);
        app.addChild(app.TEXT, from);
        trip.addChild(trip.ELEMENT, app);
        app=new Element();
        app.setName(this.DATE_TAG);
        app.addChild(app.TEXT, date+'T'+time);
        trip.addChild(trip.ELEMENT, app);
        app=new Element();
        app.setName(this.COST_TAG);
        app.addChild(app.TEXT, ""+cost);
        trip.addChild(trip.ELEMENT, app);
        
        
        Element owner=new Element();//owner
        owner.setName(this.owner);
        app=new Element();
        app.setName(this.USER_NICK_TAG);
        app.addChild(app.TEXT, this.owner);
        trip.addChild(owner.ELEMENT, app);
        app=new Element();
        app.setName(this.USER_FEED_TAG);
        app.addChild(app.TEXT, ""+this.feedbacks);
        trip.addChild(owner.ELEMENT, app);
        app=new Element();
        app.setName(this.USER_ID_TAG);
        app.addChild(app.TEXT, ""+this.getUserId());
        trip.addChild(owner.ELEMENT, app);
        trip.addChild(trip.ELEMENT, owner);
        return null;
    }

    public boolean isRepetition() {
        return repetition;
    }

    public void setRepetition(boolean repetition) {
        this.repetition = repetition;
    }

    public int getEstimatedTime() {
        return estimatedTime;
    }

    public void setEstimatedTime(int estimatedTime) {
        this.estimatedTime = estimatedTime;
    }
}
